package com.bonc.rms.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bonc.rms.entity.UserEntity;
import com.bonc.rms.jpa.UserRepository;
import com.bonc.rms.service.UserService;

/**
 * UserServie 接口实现类
 * 
 * @author zlq
 *
 */
@Service(value = "userServiceImpl")
@Transactional
public class UserServiceImpl implements UserService {

	@Autowired
	private UserRepository userRepository;

	@Override
	public List<UserEntity> findAllUsers() {
		return userRepository.findAll();
	}

	@Override
	public UserEntity findUserById(Integer id) {
		return userRepository.findOne(id);
	}

	@Override
	public UserEntity saveUser(UserEntity user) {
		return userRepository.save(user);
	}

	@Override
	public void deleteUser(Integer id) {
		userRepository.delete(id);
	}

	@Override
	public List<UserEntity> findByUserNameAndDepartment(String user_name,
			String department) {
		if (user_name.equals("") && department.equals("")) {  //用户名和部门条件均为空，查找所有用户
			return userRepository.findAll();
		}
		if (user_name.equals("") && !department.equals("")) { //用户名为空，按部门查询
			return userRepository.findByDepartment(department);
		}
		if (!user_name.equals("") && department.equals("")) { //部门为空，按用户名查找
			return userRepository.findByUserName(user_name);
		}
		return userRepository.findByUserNameAndDepartment(user_name, department);
	}



	@Override
	public void updateStateById(String state, Integer id) {
		if(state.equals("正常")){
			userRepository.updateStateById("冻结", id);			
		}
		else{
			userRepository.updateStateById("正常", id);
		}

	}

	@Override
	public String findPasswdByUserName(String userName) {
		return null;
	}

	@Override
	public UserEntity findByUserName(String user_name) {
		UserEntity user = new UserEntity();
		user = userRepository.findUserEntityByUserName(user_name);
		return user;
	}


}
